/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SQL;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Administrator
 */
public class SQL {

    public static void SQL(String query) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        try {
            stmt.executeUpdate(query);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains("for key 'PRIMARY'")) {
                System.out.println("Insertion failure. Primary Key is Already Exist");
            }
        }
        conn.close();
    }

    public static String insert(String[] columns, String[] values, String table) {
        String query = "INSERT INTO `" + table + "` (";

        int colCount = columns.length;
        for (int i = 0; i < colCount - 1; i++) {
            query += "`" + columns[i] + "`,";
        }
        query += "`" + columns[colCount - 1] + "`) VALUES(";
        for (int i = 0; i < colCount - 1; i++) {
            query += "'" + values[i] + "',";
        }
        query += "'" + values[colCount - 1] + "');";

        return query;
    }

    public static String updateValue(String[] columns, String[] values, String table, String PK, String PKV) {
        String query = "UPDATE `" + table + "` SET ";
        int colCount = columns.length;
        for (int i = 0; i < colCount - 1; i++) {
            query += "`" + columns[i] + "`='" + values[i] + "',";
        }
        query += "`" + columns[colCount - 1] + "`='" + values[colCount - 1] + "' WHERE ";
        query += "`" + PK + "`='" + PKV + "';";
        //UPDATE `weibonews`.`wb_interest` SET `interest2`='233', `rate2`=21 WHERE `uid`='1';
        return query;
    }
    public static boolean Query(String[] select,String[] column,String[] value,String table) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
        String query="SELECT ";
        int scount=select.length;
        for(int i=0;i<scount-1;i++){
            query+="`"+select[i]+"`,";
        }
        query+="`"+select[scount-1]+"` FROM "+table+" WHERE ";
        scount=column.length;
        for(int i=0;i<scount-1;i++){
            query+="`"+column[i]+"`='"+value[i]+"' and ";
        }
        query+="`"+column[scount-1]+"`='"+value[scount-1]+"';";
        
        
         Class.forName("com.mysql.jdbc.Driver").newInstance();
        //建立到MySQL的连接
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "");
        //执行SQL语句
        Statement stmt = (Statement) conn.createStatement();
        ResultSet rs=stmt.executeQuery(query);   
        if(rs.next()){
            conn.close();
            return true;
        }else{
            conn.close();
            return false;
        }       
    }
    
}
